// 导入数据库操作模块
const db = require('../db/index')
const moment = require('moment')
const {func} = require('joi')

// 获取申请类型与数量
exports.getCategoryAndNumber = (req, res) => {
    // 获取助学金申请数量
    const getBursaryNumber = (status) => {
        return new Promise(resolve => {
            const sql = status ?
                'select count(*) as count from bursary_apply where status = ?' :
                'select count(*) as count from bursary_apply';
            db.query(sql, status, (err, result) => {
                if (err || !result || result.length === 0) {
                    resolve(0); // 如果查询出错或结果为空，返回0
                } else {
                    resolve(result[0].count);
                }
            });
        });
    };

    // 获取奖学金申请数量
    const getScholarshipNumber = (status) => {
        return new Promise(resolve => {
            const sql = status ?
                'select count(*) as count from scholarship_apply where status = ?' :
                'select count(*) as count from scholarship_apply';
            db.query(sql, status, (err, result) => {
                if (err || !result || result.length === 0) {
                    resolve(0); // 如果查询出错或结果为空，返回0
                } else {
                    resolve(result[0].count);
                }
            });
        });
    };

    async function getAll() {
        const types = [
            '奖学金申请总数',  // 新增
            '助学金申请总数',  // 新增
            '辅导员审核通过',
            '辅导员审核未通过',
            '审核小组审核通过',
            '审核小组审核未通过',
            '院领导审核通过',
            '院领导审核未通过'
        ];

        const statuses = [null, null, 2, -2, 1, -1, 3, -3];  // 新增两个null表示总数

        const numbers = await Promise.all(statuses.map((status, index) => {
            if (index === 0) {
                // 查询奖学金总数
                return getScholarshipNumber(null);
            } else if (index === 1) {
                // 查询助学金总数
                return getBursaryNumber(null);
            } else {
                // 查询特定状态的数量
                return Promise.all([
                    getBursaryNumber(status),
                    getScholarshipNumber(status)
                ]).then(([bursaryCount, scholarshipCount]) => bursaryCount + scholarshipCount);
            }
        }));

        res.send({
            types: types,
            numbers: numbers
        });
    }
    getAll()
}


// 获取不同角色与数量
exports.getAdminAndNumber = (req, res) => {
    // 获取不同角色的数量
    const getNumber = identity => {
        return new Promise(resolve => {
            const sql = 'select * from users where identity = ?'
            db.query(sql, identity, (err, result) => {
                resolve(result.length)
            })
        })
    }

    async function getAll() {
        const data = [{
            value: 0,
            name: '超级管理员'
        },
            {
                value: 0,
                name: '管理员'
            },
            {
                value: 0,
                name: '用户'
            }
        ]

        for (let i = 0; i < data.length; i++) {
            data[i]['value'] = await getNumber(data[i]['name'])
        }
        res.send({
            data: data
        })
    }

    getAll()
}

// 获取不同消息等级与数量
exports.getLevelAndNumber = (req, res) => {
    // 获取不同消息等级的数量
    const getNumber = message_level => {
        return new Promise(resolve => {
            const sql = 'select * from message where message_level = ?'
            db.query(sql, message_level, (err, result) => {
                resolve(result.length)
            })
        })
    }

    async function getAll() {
        const data = [{
            value: 0,
            name: '一般'
        },
            {
                value: 0,
                name: '重要'
            },
            {
                value: 0,
                name: '必要'
            },
        ]

        for (let i = 0; i < data.length; i++) {
            data[i]['value'] = await getNumber(data[i]['name'])
        }
        res.send({
            data: data
        })
    }

    getAll()
}

// 返回每天登录人数
exports.getDayAndNumber = (req, res) => {
    // 获取最近七天日期
    const getDay = () => {
        let day = new Date()
        let week = []
        for (let i = 0; i < 7; i++) {
            // day.getDate() 返回当前的一天 比如 2023年9月24日 会返回 24 为了获取前七天的数据 这边就-1
            day.setDate(day.getDate() - 1)
            // 2023/9/23 → 2023-9-23 2023-09-23
            // moment.js
            week.push(moment(day.toLocaleDateString().replace(/\//g, '-'), 'YYYY-MM-DD').format('YYYY-MM-DD'))
        }
        return week
    }
    // 获取每天登录的人数
    const getNumber = login_time => {
        return new Promise(resolve => {
            const sql = `select * from login_log where login_time like '%${login_time}%'`
            db.query(sql, login_time, (err, result) => {
                resolve(result.length)
            })
        })
    }

    async function getAll() {
        let week = getDay()
        let number = []
        for (let i = 0; i < week.length; i++) {
            number[i] = await getNumber(week[i])
        }
        res.send({
            number: number,
            week: week
        })
    }
    getAll()
}


// 获取不同类型的建议数量
exports.getFeedbackAndNumber = (req, res) => {
    // 获取特定类型的建议数量
    const getNumber = type => {
        return new Promise(resolve => {
            const sql = 'select count(*) as count from feedback where type = ?'
            db.query(sql, type, (err, result) => {
                if (err || !result || result.length === 0) {
                    resolve(0); // 如果查询出错或结果为空，返回0
                } else {
                    resolve(result[0].count);
                }
            });
        });
    };

    async function getAll() {
        const data = [{
            value: 0,
            name: '建议'
        },
            {
                value: 0,
                name: '举报'
            },
            {
                value: 0,
                name: '问题'
            }
        ];

        for (let i = 0; i < data.length; i++) {
            data[i]['value'] = await getNumber(data[i]['name']);
        }

        res.send({
            data: data
        });
    }

    getAll();
};